package com.upay.android.client.activity;

import static com.upay.android.client.helper.Keys.PASSWORD;
import static com.upay.android.client.helper.Keys.USERID;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.type.TypeReference;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.upay.android.client.R;
import com.upay.android.client.handler.CallbackHandler;
import com.upay.android.client.handler.SignInCallbackHandler;
import com.upay.android.client.validation.EmailValidator;
import com.upay.android.client.validation.PasswordValidator;
import com.upay.android.core.service.RestService;
import com.upay.android.domain.rest.RestRequest;
import com.upay.android.util.rest.RequestMethod;
import com.upay.android.util.rest.UrlHelper;
import com.upay.domain.authentication.AuthenticationToken;
import com.upay.domain.response.ResponseEnvelope;

public class SignInActivity extends AbstractUPayActivity {
	
	/* ---- Constants ---- */
	public static final int MENU_CREATE = 0; 
	
	/* ---- Instance Variables ---- */
	private EmailValidator emailValidator = null;
	private PasswordValidator passwordValidator = null;
	
    private final CallbackHandler<AuthenticationToken> signInCallbackHandler = new SignInCallbackHandler(this);

	
	/* ---- Constructors ---- */
	
	/* ---- Business Methods ---- */
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.emailValidator = new EmailValidator();
        this.passwordValidator = new PasswordValidator();
        setContentView(R.layout.signin);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.signinmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getOrder()) {
    	    case MENU_CREATE:
    	    	Intent intent = new Intent(this, CreateAccountActivity.class);
    	    	startActivity(intent);
    	    	return true;
    	}
    	return super.onOptionsItemSelected(item);

    }
    
    public void onSignIn(View view) {
    	
    	// Show In Progress Dialog
    	//this.showProgressDialog();
    	
    	// Retrieve credentials
        String userId = ((EditText) findViewById(R.id.userid)).getText().toString();
        String password = ((EditText) findViewById(R.id.password)).getText().toString();
        
    	// Validate credentials
        Map<String, String> errors = new HashMap<String, String>();
        emailValidator.validate(userId, errors);
        passwordValidator.validate(password, errors);
        if (errors.size() > 0) {
        	this.showValidationErrors("Input fields are invaldid");
        } else  {
             // Create Request
       	    ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
       	    params.add(new BasicNameValuePair(USERID, userId));
       	    params.add(new BasicNameValuePair(PASSWORD, password));
       	    RestRequest restRequest = new RestRequest(RequestMethod.POST, UrlHelper.SIGNIN, params);
       	    TypeReference<ResponseEnvelope<AuthenticationToken>> typeReference = new TypeReference<ResponseEnvelope<AuthenticationToken>>() {};
       	     
       	    // Execute Request
       	    RestService<AuthenticationToken> restService = new RestService<AuthenticationToken>(restRequest, typeReference, signInCallbackHandler);
       	    restService.execute();
        }

    }
    
}
